package com.system.cipher;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	private MessageDigest algorithm;

	public MD5() {
		try {
			this.algorithm = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	/**
	 * VDSP自己的一个摘要算法
	 * 
	 * @param info
	 *            待摘要的信息串
	 * @return 经过摘要的信息串
	 * @throws java.lang.Exception
	 */
	public String getMD5ofStr(String info) {
		this.algorithm.update(info.getBytes());
		byte[] digesta = this.algorithm.digest();
		return byte2hex(digesta);
	}

	/**
	 * 对输入字节串进行置换
	 * 
	 * @param b
	 *            待置换的字节串
	 * @return 经过置换的字符串
	 */
	private String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}

	public static void main(String[] args) {
		MD5 md5 = new MD5();
		System.out.println(md5.getMD5ofStr("20130618130129"));
	}
}